// pages/login/login.js
const app = getApp()
const ipconfig = app.globalData.ipconfig;
Page({
    data: {
        pickerHidden: true,
        chosen: '',
        userinfo: {},
        flag: false,
        self: {}
    },
    onLoad: function () {
        this.setData({
            self: wx.getStorageSync('userInfo')
        })
    },
    //文件上传
    uploadAvatar: function (e) {
        const that = this;
        wx.chooseMedia({
            count: 1,
            sizeType: ['original', 'compressed'],
            sourceType: ['album', 'camera'],
            mediaType: ['image', 'video'],
            maxDuration: 30,
            camera: 'back',
            success: function (res) {
                var tempFilePath = res.tempFiles[0].tempFilePath;
                console.log(tempFilePath);
                wx.uploadFile({
                    url: 'http://' + ipconfig + ':8080/tool/upload',
                    filePath: tempFilePath,
                    name: 'file',
                    header: {
                        'Authorization': wx.getStorageSync('token')
                    },
                    success: function (res) {
                        const data1 = JSON.parse(res.data)
                        that.setData({
                            'userinfo.avatar': "http://rpv9gdn1y.hn-bkt.clouddn.com/" + data1.data,
                            flag: !that.data.flag
                        })
                    },
                    fail: function (err) {
                        console.log(err);
                    }
                });
            },
            fail: function (err) {
                console.log(err);
            }
        })
    },
    login() {
        let that = this;
        // 登录
        wx.login({
            success(res) {
                if (res.code) {
                    console.log("code:", res.code);
                    //发起网络请求
                    wx.request({
                        url: 'http://' + ipconfig + ':8080/mini/login',
                        data: {
                            code: res.code
                        },
                        success: (res) => {
                            console.log("111111111", res.data.data);
                            const {
                                flag,
                                data,
                                message
                            } = res.data;
                            if (!flag) {
                                wx.showToast({
                                    title: message,
                                    icon: 'fail',
                                    duration: 2000
                                })
                            }
                            wx.setStorageSync('token', `${data.tokenHead} ${data.token}`);
                            wx.setStorageSync('userInfo', data.userInfo);
                            wx.setStorageSync('openId', data.openid)
                        },
                        fail: (err) => {
                            console.log("请求失败", err);
                        }
                    })
                } else {
                    console.log('登录失败！' + res.errMsg)
                }
            }
        });
    },


    pickerConfirm(e) {
        this.setData({
            pickerHidden: true
        })
        this.setData({
            chosen: e.detail.value
        })
    },

    pickerCancel() {
        this.setData({
            pickerHidden: true
        })
    },

    pickerShow() {
        this.setData({
            pickerHidden: false
        })
    },
    formSubmit(e) {
        console.log(e)
        const user = e.detail.value;
        const that = this;
        this.setData({
            'userinfo.sex': user.sex,
            'userinfo.username': user.username,
            'userinfo.password': user.password,
            'userinfo.nickName': user.nickName,
            'userinfo.address': user.address,
            'userinfo.phoneNumber': user.phoneNumber,
            'userinfo.email': user.email,
            'userinfo.openId': this.data.self.openId
        })
        wx.request({
            url: 'http://' + ipconfig + ':8080/mini/update/info',
            method: 'POST',
            data: this.data.userinfo,
            success: function (res) {
                console.log(res)
                wx.clearStorageSync()
                that.login()
            },
            fail: function (err) {
                console.log(err);
            }
        })
    },
    formReset(e) {
        console.log('form发生了reset事件，携带数据为：', e.detail.value)
        this.setData({
            chosen: ''
        })
    }
})